import os
import logging
import mysql.connector
from pathlib import Path

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(message)s'
)

logger = logging.getLogger(__name__)

def init_database():
    """初始化数据库"""
    try:
        # 读取SQL文件
        sql_file = Path(__file__).parent / 'init.sql'
        if not sql_file.exists():
            raise FileNotFoundError(f"SQL文件不存在: {sql_file}")
            
        with open(sql_file, 'r', encoding='utf-8') as f:
            sql_commands = f.read()
        
        # 连接MySQL
        conn = mysql.connector.connect(
            host='localhost',
            user='root',
            password='123456'
        )
        
        cursor = conn.cursor()
        
        try:
            # 执行SQL命令
            for command in sql_commands.split(';'):
                command = command.strip()
                if command:
                    cursor.execute(command)
                    
            conn.commit()
            logger.info("数据库初始化成功")
            
        except Exception as e:
            conn.rollback()
            raise e
            
        finally:
            cursor.close()
            conn.close()
            
    except Exception as e:
        logger.error(f"数据库初始化失败: {e}", exc_info=True)
        raise

if __name__ == '__main__':
    init_database() 